package com.kzd.gtgame.main.home

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.palette.graphics.Palette
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setTransitionStatusT
import com.kzd.gtgame.entity.Doings
import com.kzd.gtgame.entity.DoingsTop
import com.kzd.gtgame.entity.GameDoing
import com.kzd.gtgame.jetpack.vm.HomeViewModel
import com.kzd.gtgame.main.KEY_DOINGS_DOING
import com.kzd.gtgame.main.KEY_DOINGS_TITLE
import com.kzd.gtgame.main.KEY_DOINGS_TYPE
import com.kzd.gtgame.main.game.adapter.Game1Adapter
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.image.loadCenterImage
import com.module.lemlin.image.loadImage
import com.module.lemlin.image.loadRoundImage
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_home_doings.*
import kotlinx.android.synthetic.main.include_app_refresh.*

class HomeDoingsActivity : OwnerViewModeActivity<HomeViewModel>() {

    private val mDoingTitle: String? by lazy {
        intent?.extras?.getString(KEY_DOINGS_TITLE)
    }

    private val mDoingMark: String? by lazy {
        intent?.extras?.getString(KEY_DOINGS_TYPE)
    }

    private val mDoingDoing: Doings<GameDoing>? by lazy {
        intent?.extras?.getSerializable(KEY_DOINGS_DOING) as? Doings<GameDoing>
    }

    private var mStart: Long = 0

    private val mGame1Adapter: Game1Adapter<GameDoing> by lazy {
        Game1Adapter()
    }

    override fun viewModel(): HomeViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_home_doings

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flHomeDoingsBar)
    }

    override fun initView() {

        flHomeDoingsBar.setTitleTextContent(mDoingTitle)

        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mGame1Adapter

        mSmartRefreshLayout.setOnRefreshListener {
            val layout = mDoingMark
                ?: return@setOnRefreshListener
            viewModel.homeDoings(layout)
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            val layout = mDoingMark
                ?: return@setOnLoadMoreListener
            viewModel.homeDoings(layout, mStart, 2, (true))
        }
    }

    override fun initData() {
        if (mDoingDoing == null) mDoingMark?.let {
            viewModel.homeDoings(it)
        } else mDoingDoing?.let {
            fillData(it)
        }
    }

    override fun initViewMode() {
        viewModel.homeDoingsLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusT(response, mGame1Adapter, mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            mStart = data.start
            if (isLoadMore) {
                mGame1Adapter.addData(data.list)
            } else {
                fillData(data)
            }
        })
    }

    private fun fillData(doing: Doings<GameDoing>) {
        mGame1Adapter.setHeaderView(createHeaderView(doing.top))
        clHomeDoingsBack.setBackgroundColor(Color.parseColor(doing.top.color))
//        setOnPageChangeHeader(clHomeDoingsBack, "")
        mGame1Adapter.setNewInstance(doing.list)
        mStart = doing.start
    }

    private fun createHeaderView(top: DoingsTop): View {
        val view = LayoutInflater.from(this as Context)
            .inflate(R.layout.item_doings_header, null)
        view.findViewById<TextView>(R.id.tvDoingsHeaderContent)
            .text = top.content
        view.findViewById<ImageView>(R.id.ivDoingsHeaderImage)
            .loadImage(top.image)
        return view
    }


    private fun setOnPageChangeHeader(view: View, image: String) =
        Glide.with(this).asBitmap().load(image).into(object : CustomTarget<Bitmap>() {
            override fun onResourceReady(
                resource: Bitmap, transition: Transition<in Bitmap>?
            ) {
                setPalette(resource)
            }

            override fun onLoadCleared(placeholder: Drawable?) = Unit

            fun setPalette(resource: Bitmap) = runOnUiThread {
                Palette.from(resource).generate {
                    val colorsArray = intArrayOf(
                        it?.lightMutedSwatch?.rgb ?: 0, it?.lightMutedSwatch?.rgb ?: 0
                    )
                    val drawable = GradientDrawable().apply {
                        orientation = GradientDrawable.Orientation.TOP_BOTTOM
                        colors = colorsArray
                    }
                    view.background = drawable
                }
            }
        })
}